/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.heyma.webapp.dao;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * @author John
 */
@Repository
public class DaoImpl implements Dao {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void save(Object obj) {
        getCurrentSession().save(obj);
    }

    @Override
    public void persist(Object obj) {
        getCurrentSession().persist(obj);
    }

    @Override
    public void update(Object obj) {
        getCurrentSession().merge(obj);
    }

    @Override
    public void delete(Object obj) {
        getCurrentSession().delete(obj);
    }

    @Override
    public <T> List<T> findAll(Class<T> en) {
        return getCurrentSession().createCriteria(" from " + en.getClass()).list();
    }

    @Override
    public <T> Object findOne(Class<T> en, int id) {
        return getCurrentSession().get(en, id);
    }
}
